#include"global.h"
#include"testset.h"
#include"function.h"


testset::testset(const string& filename)
{
	try{
	_Name=filename;
	ifstream _in(filename);
	if(!_in.is_open())
		throw msg((filename+":cannot open file").c_str());
	string _line;
	while(_in>>_line)
		_Data.push_back(_line);
	_in.close();
	}catch(msg &e){simpleio::error("testset: cannot read testsets",e);}
	//cout<<"\nCIR["<<filename<<"]\nlength="<<length()<<"\nnum="<<num()<<"\n";
}


testset::~testset(void)
{
}

//0,1,X
tuple<int,int,int> testset::count(const string& str)
{
	int zero=0;
	int one=0;
	int x=0;
	for(auto &e:str)
		if(e=='0')
			zero++;
		else if(e=='1')
			one++;
		else if(e=='X')
			x++;
		if(zero+one+X!=str.length())throw msg("count error");
		return make_tuple(zero,one,X);
}

double testset::ratio(int original,int generate)
{
	return (1-(double)generate/original)*100.0;
}

unique_ptr<string> testset::data_string()const
{
	unique_ptr<string> _str(new string);
	for(auto &v:_Data)
		_str->append(v);
	return _str;
}

unique_ptr<vector<string>> testset::data_reverse()const
{
	unique_ptr<vector<string>> rdata(new vector<string>);
	for(int j=0;j<length();j++)
	{
		string _s;
		for(int i=0;i<num();i++)
			_s+=_Data.at(i).at(j);
		rdata->push_back(_s);
	}
	if(rdata->size()!=length()||rdata->back().length()!=num())
		throw msg("testset::data_reverse() invalid size");
	return rdata;
}